<template>
    <keep-alive>
    <d2-container>
        <el-form :inline="true" :model="dataSearch" ref="dataSearch">

            <el-form-item label="课程名称" prop="name">
                <el-input v-model="dataSearch.name" placeholder="请输入课程名称"></el-input>
            </el-form-item>
                         <el-form-item label="校区" prop="school" v-if='showSchool'>
                <el-select
                        v-model="dataSearch.school"
                        filterable
                        placeholder="请选择校区"
                        @change="schoolSelected"
                        :disabled="disabled"
                >
                    <el-option
                            v-for="item in schoolList"
                            :key="item._id"
                            :label="item.name"
                            :value="item._id"
                    ></el-option>
                </el-select>
            </el-form-item>

            <el-form-item>
                <el-button type="primary" @click="formSubmit">
                    <d2-icon name="search"/>
                    查询
                </el-button>
            </el-form-item>
            <el-form-item>
                <el-button @click="formReset">
                    <d2-icon name="refresh"/>
                    重置
                </el-button>
            </el-form-item>
            <a href="#/course-new">
                <el-button type="primary" style="float:right" icon="el-icon-plus">新建课程</el-button>
            </a>
        </el-form>
        
        

        <el-table :data="dataList" border style="width: 100%">
            <el-table-column prop="name" label="课程名称"></el-table-column>
            <el-table-column prop="courseType" label="课程类型"></el-table-column>
            <el-table-column prop="pricePerClass" label="单课价格(元)"></el-table-column>
            <el-table-column prop="pricePerHour" label="每小时价格(元)"></el-table-column>
            <el-table-column prop="pricePerMonth" label="每月价格(元)"></el-table-column>
            <el-table-column prop="remark" label="课程备注"></el-table-column>
            <el-table-column prop="duration" label="时长(小时)"></el-table-column>
            <el-table-column prop="school" label="开设学校"></el-table-column>

            <el-table-column fixed="right" label="操作">
                <template slot-scope="scope">
                    <el-button @click="editClick(scope.row)" type="text" size="small">修改</el-button>
                    <el-button @click="deleteClick(scope.row)" type="text" size="small">删除</el-button>
                </template>
            </el-table-column>
        </el-table>
        <el-pagination
                background
                layout="total,sizes,prev, pager, next,jumper"
                :page-size="pageSize"
                :current-page="currentPage"
                :total="total"
                @current-change="handleCurrentChange"
                @size-change="handlerSizeChange"
                @next-click="handleNextPage"
                @prev-click="handlePrevClick"
        ></el-pagination>
    </d2-container>
</keep-alive >
</template>

<script>
    import request from "@/plugin/axios";
    import {mapState} from "vuex";

    export default {
        data() {
            return {
                dataList: [],
                total: 100,
                pageSize: 20,
                currentPage: 1,
                courseType: [''],
                school: [''],
                dataSearch: {school:undefined},
                courseTypeDetail: [],
                schoolList : [],
                showSchool :false,
                heaaquarter : 0,
            };
        },
        computed: {
            ...mapState('d2admin/user', ['info']),
        },
        methods: {
            formSubmit() {
                this.dataSearch.page = this.currentPage;
                this.dataSearch.pageSize = this.pageSize;
                this.getDataList(this.dataSearch);
            },
            formReset() {
                this.$refs.dataSearch.resetFields();
            },
            editClick(e){
                this.$router.push({path:'/course-new?id=' + e._id})
                // this.$navigation.on('forward',(to,from)=>{})
            },

            deleteClick(e) {
                this.$confirm("此操作将删除该课程, 是否继续?", "提示", {
                    confirmButtonText: "确定",
                    cancelButtonText: "取消",
                    type: "warning"
                })
                    .then(() => {
                        request({
                            url: "/course/deleteCourse",
                            method: "post",
                            data: { _id: e._id }
                        }).then(res => {
                            if(res.code ==200){
                                this.$message({
                                    message: "删除成功"
                                });
                                this.getDataList(this.dataSearch);
                            }
                        });

                    })
                    .catch(() => {
                        this.$message({
                            type: "info",
                            message: "已取消删除"
                        });
                    });
            },


            handleCurrentChange(currentPage) {
                this.currentPage = currentPage
                this.dataSearch.page = this.currentPage;
                this.dataSearch.pageSize = this.pageSize;
                this.getDataList(this.dataSearch);
            },
            handlerSizeChange(pageSize) {
                this.pageSize = pageSize
                this.dataSearch.page = this.currentPage;
                this.dataSearch.pageSize = this.pageSize;
                this.getDataList(this.dataSearch);
            },
            handleNextPage() {
                this.currentPage = this.currentPage + 1
                this.dataSearch.page = this.currentPage;
                this.dataSearch.pageSize = this.pageSize;
                this.getDataList(this.dataSearch);
            },
            handlePrevClick() {
                this.currentPage = this.currentPage - 1
                this.dataSearch.page = this.currentPage;
                this.dataSearch.pageSize = this.pageSize;
                this.getDataList(this.dataSearch);
            },

                        getSchoolList() {
                let data = {};
                data.page = 1
                data.pageSize = 99999
                request({
                    url: "/school/getSchoolList",
                    method: "post",
                    data
                }).then(res => {
                    this.schoolList = res.data.school;
                    // 返回数据
                });
            },

            getDataList(data) {

                // this.dataSearch.limitSchool = this.info.school.map(value => value._id);
                // data.limitSchool = this.dataSearch.limitSchool;
                // if (this.headquarter == 1){
                data.limitSchool = this.dataSearch.school
                // }
                request({
                    url: "/course/getCourseList",
                    method: "post",
                    data: data
                }).then(res => {

                    this.dataList = [];
                    this.dataList = res.data.course;
                    this.dataList.forEach((item, index) => {
                        let courseType = [];
                        let school = [];
                        courseType = item.courseType.map(value => {
                            return value.name
                        })
                        // school = item.school.map(value => {
                        //     return value.name
                        // })
                        this.dataList[index]['courseType'] = courseType.join(',')
                        this.dataList[index]['school'] = item.school.name
                    })
                    this.total = res.data.total;
                    this.currentPage = res.data.currentPage;
                })
            },
            load(){
                 this.dataSearch.page = this.currentPage;
                this.dataSearch.pageSize = this.pageSize;
                this.getDataList(this.dataSearch);
                // this.headquarter = this.info.school[0].headquarter == 1? 1 : 0
                this.school = this.info.school[0]._id
                this.dataSearch.school = this.school
                this.rights = this.info.rights.map(function(e){return e.name})

                let index1 = this.rights.indexOf('root') == -1
                let index2 = this.rights.indexOf('授课老师') != -1
                let index4 = this.rights.indexOf('授课老师') != -1
                index2 = (index2 || index4)
                let index3 = this.rights.indexOf('教务中心') == -1
                let index5 = this.rights.indexOf('校长') == -1
                let index = (index1 && index5)

                if (index != -1){
                    this.headquarter = 1
                    this.getSchoolList()
                    this.showSchool = true
                    this.dataSearch.school = this.info.school[0]._id
                }
            }
        },
        
        beforeRouteEnter(to, from, next) {
            if (to.meta.refresh == 'a'){
                to.meta.refresh = undefined
                next(v=>{
                v.load()
                })
            }
            else{
                next()
            }


        },
        beforeRouteLeave(to,from,next){
            next()
        },
       
        
        created() {
            // if (this.$route.meta.refresh){
           this.load()
        }
        // }
    };
</script>
